package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.QiNiuOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    @Autowired
    private QiNiuOssUtil qiNiuOssUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        try {
            String OriginalFilename = file.getOriginalFilename();
            //截掉文件后缀
            String suffix = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
//            if (!suffix.equals(".jpg") && !suffix.equals(".png") && !suffix.equals(".jpeg")) {
//                return Result.error("上传文件格式不正确");
//            }
            //用UUID生成文件名
            String fileName = UUID.randomUUID().toString() + suffix;
            String url = qiNiuOssUtil.upload(file.getInputStream(), fileName);
            System.out.println(url);
            return Result.success(url);
        } catch (Exception e) {
            log.error("上传文件失败", e);
        }
        return Result.error("上传文件失败");
    }


}
